/* $RCSFile: EnhancedHtmlDB.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/AddonNice/PageModules/EnhancedHTML/EnhancedHtmlDB.cs $
 * $log$
 * Revision 19 2010/08/09 09:43:53  christian.surieux
 *   Added Log to cs sources files
 * Revision 6 2010/08/06 19:56:25  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;

using AddonNice.Configuration;
using AddonNice.Diagnostics;

namespace AddonNice.Settings.PageModules.EnhancedHtml
{
	/// <summary>
	/// AddonNice EnhancedHtml Module
	/// Class that encapsulates all data logic necessary to add/query/delete
	/// EnhancedHtml Pages within the Portal database.
	/// </summary>
    public class EnhancedHtmlDB  : DBDataStore
    {
		/// <summary>
        /// The GetAllPages method returns a SqlDataReader containing all of the
        /// pages for a specific portal module from database.
		/// </summary>
        public SqlDataReader GetAllPages(int moduleID) 
        {
            SqlCommand myCommand            = new SqlCommand("dbo.adn_GetEnhancedHtml", myConnection);
            myCommand.CommandType           = CommandType.StoredProcedure;

            AddModuleIDParameters(myCommand,moduleID);

			SqlDataReader result			= null;
			try
			{
				myConnection.Open();
				result						= myCommand.ExecuteReader(CommandBehavior.CloseConnection);
			}
			catch(Exception exp)
			{
				if (DBTraceSwitch.Sw.TraceError)
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"EnhancedHtmlDB GetAllPages moduleID : {0}, ex : {1}",moduleID,exp),DBTraceSwitch.Sw.Info);
				if ( result != null )
					result.Close();
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
				throw;
			}
			return result;
		}

		/// <summary>
		/// The GetAllPages method returns a SqlDataReader containing all of the
		/// pages for a specific portal module from database.
		/// </summary>
		public SqlDataReader GetLocalizedPages(int moduleID, int cultureCode) 
		{
			SqlCommand myCommand = new SqlCommand("dbo.adn_GetEnhancedLocalizedHtml", myConnection);
			myCommand.CommandType = CommandType.StoredProcedure;

            AddModuleIDParameters(myCommand,moduleID);

			SqlParameter parameterCultureCode = new SqlParameter("@CultureCode", SqlDbType.Int, 4);
			parameterCultureCode.Value = cultureCode;
			myCommand.Parameters.Add(parameterCultureCode);


			SqlDataReader result			= null;
			try
			{
				myConnection.Open();
				result						= myCommand.ExecuteReader(CommandBehavior.CloseConnection);
			}
			catch(Exception exp)
			{
				if (DBTraceSwitch.Sw.TraceError)
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"EnhancedHtmlDB GetLocalizedPages moduleID : {0}, ex : {1}",moduleID,exp),DBTraceSwitch.Sw.Info);
				if ( result != null )
					result.Close();
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
				throw;
			}
			return result;
		}

		/// <summary>
		/// The GetSinglePage method returns a SqlDataReader containing details
		/// about a specific page from the EnhancedHtml database table.
		/// </summary>
        public SqlDataReader GetSinglePage(int itemID) 
        {
            SqlCommand myCommand = new SqlCommand("dbo.adn_GetSingleEnhancedHtml", myConnection);

            myCommand.CommandType = CommandType.StoredProcedure;

            AddItemIDParameters(myCommand,itemID);

			SqlDataReader result			= null;
			try
			{
				myConnection.Open();
				result						= myCommand.ExecuteReader(CommandBehavior.CloseConnection);
			}
			catch(Exception exp)
			{
				if (DBTraceSwitch.Sw.TraceError)
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"EnhancedHtmlDB GetSinglePage itemID : {0}, ex : {1}",itemID,exp),DBTraceSwitch.Sw.Info);
				if ( result != null )
					result.Close();
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
				throw;
			}
			return result;
		}

		/// <summary>
		/// The DeleteLink method deletes a specified page from
		/// the EnhancedHtml database table.
		/// </summary>
        public void DeletePage(int itemID) 
        {
            SqlCommand myCommand = new SqlCommand("dbo.adn_DeleteEnhancedHtml", myConnection);
            myCommand.CommandType = CommandType.StoredProcedure;

            AddItemIDParameters(myCommand,itemID);

			try
			{
				myConnection.Open();
				myCommand.ExecuteNonQuery();
			}
			catch(Exception exp)
			{
				if (DBTraceSwitch.Sw.TraceError)
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"EnhancedHtmlDB DeletePage itemID:{0}, ex : {1}",itemID, exp),DBTraceSwitch.Sw.Info);
				throw;
			}
			finally
			{
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
			}

		}

		/// <summary>
		/// The AddPage method adds a new page within the
		/// EnhancedHtml database table, and returns ItemID value as a result.
		/// </summary>
        public int AddPage(int moduleID, string userName,string title,int viewOrder,int cultureCode,string desktopHtml) 
        {
            if (userName.Length < 1) 
            {
                userName                        =   StStr.unknown;
            }
            SqlCommand myCommand                =   new SqlCommand("dbo.adn_AddEnhancedHtml", myConnection);
            myCommand.CommandType               =   CommandType.StoredProcedure;

            SqlParameter parameterItemID		=	new SqlParameter(StStr.ATItemID, SqlDbType.Int, 4);
            parameterItemID.Direction           =   ParameterDirection.Output;
            myCommand.Parameters.Add(parameterItemID);

            AddModuleIDParameters(myCommand,moduleID);

            SqlParameter parameterUserName      =   new SqlParameter("@UserName", SqlDbType.NVarChar, 100);
            parameterUserName.Value             =   userName;
            myCommand.Parameters.Add(parameterUserName);

            SqlParameter parameterTitle         =   new SqlParameter("@Title", SqlDbType.NVarChar, 100);
            parameterTitle.Value                =   title;
            myCommand.Parameters.Add(parameterTitle);

			SqlParameter parameterViewOrder     =   new SqlParameter("@ViewOrder", SqlDbType.Int, 4);
			parameterViewOrder.Value            =   viewOrder;
			myCommand.Parameters.Add(parameterViewOrder);

			SqlParameter parameterCultureCode   =   new SqlParameter("@CultureCode", SqlDbType.Int, 4);
			parameterCultureCode.Value          =   cultureCode;
			myCommand.Parameters.Add(parameterCultureCode);
			
			SqlParameter parameterDesktopHtml   =   new SqlParameter("@DesktopHtml", SqlDbType.NVarChar,desktopHtml.Length);
            parameterDesktopHtml.Value          =   desktopHtml;
            myCommand.Parameters.Add(parameterDesktopHtml);
			
            int ret	                            =	-1;
			try
			{
				myConnection.Open();
				myCommand.ExecuteNonQuery();
				ret	                            =	(int)parameterItemID.Value;
			}
			catch(Exception exp)
			{
				if (DBTraceSwitch.Sw.TraceError)
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"EnhancedHtmlDB AddPage itemID:{0}, ex : {1}",title,exp),DBTraceSwitch.Sw.Info);
				throw;
			}
			finally
			{
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
			}
            return ret;
        }

		/// <summary>
		/// The UpdateLink method updates a specified page within
		/// the EnhancedHtml database table.
		/// </summary>
        public void UpdatePage(int moduleID, int itemID, String userName, String title, int viewOrder, int cultureCode, String desktopHtml) 
        {
            if (userName.Length < 1) 
            {
                userName                        =   StStr.unknown;
            }

            SqlCommand myCommand                =   new SqlCommand("dbo.adn_UpdateEnhancedHtml", myConnection);
            myCommand.CommandType               =   CommandType.StoredProcedure;

            AddItemIDParameters(myCommand,itemID);

            SqlParameter parameterUserName      =   new SqlParameter("@UserName", SqlDbType.NVarChar, 100);
            parameterUserName.Value             =   userName;
            myCommand.Parameters.Add(parameterUserName);

            SqlParameter parameterTitle         =   new SqlParameter("@Title", SqlDbType.NVarChar, 100);
            parameterTitle.Value                =   title;
            myCommand.Parameters.Add(parameterTitle);

			SqlParameter parameterViewOrder     =   new SqlParameter("@ViewOrder", SqlDbType.Int, 4);
            parameterViewOrder.Value            =   viewOrder;
            myCommand.Parameters.Add(parameterViewOrder);

			SqlParameter parameterCultureCode   =   new SqlParameter("@CultureCode", SqlDbType.Int, 4);
			parameterCultureCode.Value          =   cultureCode;
			myCommand.Parameters.Add(parameterCultureCode);

			SqlParameter parameterDesktopHtml   =   new SqlParameter("@DesktopHtml", SqlDbType.NVarChar,desktopHtml.Length);
			parameterDesktopHtml.Value          =   desktopHtml;
			myCommand.Parameters.Add(parameterDesktopHtml);

			try
			{
				myConnection.Open();
				myCommand.ExecuteNonQuery();
			}
			catch(Exception exp)
			{
				if (DBTraceSwitch.Sw.TraceError)
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"EnhancedHtmlDB UpdatePage itemID:{0}, ex : {1}",itemID,exp),DBTraceSwitch.Sw.Info);
				throw;
			}
			finally
			{
				if ( myConnection.State == System.Data.ConnectionState.Open )
					myConnection.Close();
			}

		}
    }
}
